package Gui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.Spring;

import ocsf.server.ConnectionToClient;
import client.LibaryMain;
import Control.UserSubscriber;
import Gui.*;
import Logic.LoginInfo;

import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ImageIcon;
/**
 * this class implements gui of return reports
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class EditDetails extends JPanel {
	private String Username= null ;
	private static JLabel usertextField;
	private static JLabel emailtextField;
	private static JLabel TeltextField;
	public static EditDetails ed;
	/**
	 * Constructor of edit details window 
	 * @param userName
	 * @param client
	 */
	public EditDetails(String userName, ConnectionToClient client) {
		
		
		setBackground(new Color(153, 204, 255));
		Username= userName;
		ArrayList<String> Arr=new ArrayList<String>();
        Arr.add("EditDetails");
        Arr.add(Username);
		initComponents();
		LibaryMain.chat.handleMessageFromGui(Arr);
		}
	
	
	/**
	 * this function create instance of edit details gui 
	 */
	public void  initComponents() {
			
	
				
				JLabel lblUserEditDetails = new JLabel("Edit Subscriber Details");
				lblUserEditDetails.setBounds(37, 28, 318, 33);
				lblUserEditDetails.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 26));
			
				JButton btnEmailUpdate = new JButton("Email Update");
				btnEmailUpdate.setBounds(372, 265, 284, 42);
				btnEmailUpdate.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
				
				btnEmailUpdate.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						String NewEmail = JOptionPane.showInputDialog("Please Enter you new Email addrees");
						if(NewEmail==null){}
						else{	
						if(NewEmail.equals(""))
								{
								JOptionPane.showMessageDialog(null, "New Email not entered correctly, please try again");
								}
						else
								{
								Pattern p = Pattern.compile("@");
								Matcher m = p.matcher(NewEmail);
								if (!m.find()) {
												JOptionPane.showMessageDialog(null, "Please enter Email correctly");
												}
								else
												{
												ArrayList<String> Arr=new ArrayList<String>();
												Arr.add("NewEmail");
												Arr.add(Username);
												Arr.add(NewEmail);
												LibaryMain.chat.handleMessageFromGui(Arr);
												}
								}
						}// sddf
						
					}
				});
				JButton btnBack = new JButton("Back");
				btnBack.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
				btnBack.setBounds(157, 553, 79, 25);
				btnBack.addActionListener(new java.awt.event.ActionListener() {     //  back to  subscriber 
		            public void actionPerformed(java.awt.event.ActionEvent evt) {
		            	btnBackActionPerformed(evt);
		            
		            }

							private void btnBackActionPerformed(ActionEvent evt) {
								
								MainScreen.cardLayot.show(MainScreen.card, "SubscriberWindow");
								
							}
						
		        });
				
				JButton btnLogout = new JButton("Logout");
				btnLogout.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
				btnLogout.setBounds(802, 553, 79, 25);
				btnLogout.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						ArrayList<String> Arr=new ArrayList<String>();
						Arr.add("Logout");
						Arr.add("Subscriber");
						Arr.add(Username);
						LibaryMain.chat.handleMessageFromGui(Arr);
						MainScreen.cardLayot.show(MainScreen.card, "Login");
						
					}
				});
				
				JButton btnTelephoneUpdate = new JButton("Telephone Update");
				btnTelephoneUpdate.setBounds(372, 318, 284, 42);
				btnTelephoneUpdate.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
				btnTelephoneUpdate.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
					
						String NewTelephone = JOptionPane.showInputDialog("Please Enter you new Telephone number");
						if(NewTelephone==null){}
						else {
						if(NewTelephone.equals(""))
							{
							JOptionPane.showMessageDialog(null, "New Telephone not entered correctly, please try again");
							}
						else
							{
							Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
							Matcher m = p.matcher(NewTelephone);
							if (m.find()) {
											JOptionPane.showMessageDialog(null, "Please enter only numbers");
										  }
									else
										  {
											ArrayList<String> Arr=new ArrayList<String>();
											Arr.add("NewTelephone");
											Arr.add(Username);
											Arr.add(NewTelephone);
											LibaryMain.chat.handleMessageFromGui(Arr);
										  }
							}										
						}
					}
																			});
				
				
				
				
				JLabel lblSubscriber = new JLabel("Subscriber:");
				lblSubscriber.setBounds(58, 118, 122, 17);
				lblSubscriber.setFont(new Font("Tahoma", Font.BOLD, 16));
				
			
				JLabel lblEmail = new JLabel("Email:");
				lblEmail.setBounds(58, 159, 96, 17);
				lblEmail.setFont(new Font("Tahoma", Font.BOLD, 16));
				
			
				JLabel lblTelephone = new JLabel("Telephone:");
				lblTelephone.setBounds(58, 198, 122, 17);
				lblTelephone.setFont(new Font("Tahoma", Font.BOLD, 16));
				
				usertextField = new JLabel("111");
				usertextField.setFont(new Font("Tahoma", Font.BOLD, 16));
				usertextField.setBounds(210, 155, 192, 25);
				usertextField.setBackground(new Color(153, 204, 255));
				
				emailtextField = new JLabel("111");
				emailtextField.setFont(new Font("Tahoma", Font.BOLD, 16));
				emailtextField.setBounds(210, 113, 192, 26);
				emailtextField.setBackground(new Color(153, 204, 255));
				
				TeltextField = new JLabel("111");
				TeltextField.setFont(new Font("Tahoma", Font.BOLD, 16));
				TeltextField.setBounds(210, 194, 192, 25);
				TeltextField.setBackground(new Color(153, 204, 255));
				setLayout(null);
				add(btnBack);
				add(lblUserEditDetails);
				add(TeltextField);
				add(usertextField);
				add(emailtextField);
				add(btnLogout);
				add(btnEmailUpdate);
				add(btnTelephoneUpdate);
				add(lblEmail);
				add(lblSubscriber);
				add(lblTelephone);
				
				JLabel label = new JLabel("");
				label.setIcon(new ImageIcon(EditDetails.class.getResource("/Gui/images/Gmail_PNG_icon_512x512_px_size_by_ncrow.png")));
				label.setBounds(443, 56, 692, 486);
				add(label);
	}
	
	/**
	 * inserts personal information from DB to Gui
	 * @param str
	 */
public void fillForm(String[] str)
{
	usertextField.setText(str[2]);
	emailtextField.setText(str[3]);
	TeltextField.setText(str[4]);
}
}
